package com.bdqn.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model,String usrName, Long roleId, Long pageIndex){
        Subject subject= SecurityUtils.getSubject();
        pageIndex=pageIndex==null?1:pageIndex;
        List<Role> roles = roleService.list();
        Page<User> page =new Page<>();
        page.setCurrent(pageIndex);
        page=userService.findPage(usrName,roleId,page);
        model.addAttribute("roles",roles);
        model.addAttribute("userPager",page);
        return "user/list";
    }
    @RequiresPermissions("用户添加")
    @GetMapping("/add")
    public String add(Model model){
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        return "user/add";
    }

    @ResponseBody
    @GetMapping("/check")
    public Object check(@RequestParam  String usrName){
        Map resultMap=new HashMap<>();
        User user = userService.selectUserName(usrName);
        if (user!=null){
            resultMap.put("result","true");
        }
        return resultMap;
    }

    @PostMapping("/save")
    public String save(User user){
        if (user.getUsrFlag()==null){
            user.setUsrFlag(0);
        }
        int add = userService.add(user);
        if (add>0){
            return "redirect:/user/list";
        }else{
            return "user/add";
        }
    }

    @GetMapping("/edit")
    @RequiresPermissions("用户编辑")
    public String edit(@RequestParam("usrId") Long usrId,Model model){
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        User user = userService.selectId(usrId);
        model.addAttribute("user",user);
        return "user/edit";
    }

    @PostMapping("/saveUpd")
    public String upd(User user){
        if (user.getUsrFlag()==null){
            user.setUsrFlag(0);
        }
        int upd = userService.upd(user);
        if (upd>0){
            return "redirect:/user/list";
        }else{
            return "user/edit";
        }
    }
    @ResponseBody
    @PostMapping("/del/{usrId}")
    public Object del(@PathVariable long usrId){
        int del = userService.del(usrId);
        Map<Object, Object> resultMap=new HashMap<>();
        if (del>0){
            resultMap.put("result","true");
        }
        return resultMap;
    }
}
